<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Controllers
 * @see 		Hosting status
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */
class Hosting_status extends CI_Controller {
	var $uri_arr = array();
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('hiti_model', 'hiti', true);
		$this->hiti->load_config();
		if (check_rights() == false)
		{
			redirect();
		}
		$this->load->model($GLOBALS['var']['act'].'_model', 'Host', true);
		$this->uri_arr = array(
			'deleted' => $GLOBALS['var']['deleted'], 
			'q' => '',
			'rowstart' => ''
		);
	}

	public function index()
	{
		/*
		* Xu ly du lieu
		*/
		$disk = DISK_QUOTA * 1024 * 1024;

		$sqlUsage = $this->Host->dbsize();
		$pSqlUsage = round(($sqlUsage / $disk) * 100, 2);

		$codeSize = SOURCE_SIZE;
		$pCodeSize = round(($codeSize / $disk) * 100, 2);

		$upload_file = folder_size(UPLDIR);
		$pUpload_file = round(($upload_file / $disk) * 100, 2);

		$emailSize = folder_size(MAILDIR);
		$pEmailSize = round(($emailSize / $disk) * 100, 2);
		
		$diskUsage = $codeSize + $sqlUsage + $upload_file + $emailSize;

		$left = $disk - $diskUsage;
		$pLeft = round(($left / $disk) * 100, 2);

		$expireHost = strtotime(HOST_DATE_REG) + 60 * 60 * 24 * 365 * HOST_YEAR_REG;
        $dayHostUsed = (time() - strtotime(HOST_DATE_REG))  / 60 / 60 / 24;
        $pDayHostUsed = min(round(($dayHostUsed / (365 * HOST_YEAR_REG)) * 100, 2), 100);
        $dayHostLeft = round(365 * HOST_YEAR_REG - $dayHostUsed);

        $expireDomain = strtotime(DOMAIN_DATE_REG) + 60 * 60 * 24 * 365 * DOMAIN_YEAR_REG;
        $dayDomainUsed = (time() - strtotime(DOMAIN_DATE_REG))  / 60 / 60 / 24;
        $pDayDomainUsed = min(round(($dayDomainUsed / (365 * DOMAIN_YEAR_REG)) * 100, 2), 100);
        $dayDomainLeft = round(365 * DOMAIN_YEAR_REG - $dayDomainUsed);

		$data = array(
			'left' => $left,
			'pLeft' => $pLeft,
			'upload_file' => $upload_file,
			'pUpload_file' => $pUpload_file,
			'codeSize' => $codeSize,
			'pCodeSize' => $pCodeSize,
			'sqlUsage' => $sqlUsage,
			'pSqlUsage' => $pSqlUsage,
			'emailSize' => $emailSize,
			'pEmailSize' => $pEmailSize,
			'expireHost' => $expireHost,
			'dayHostUsed' => $dayHostUsed,
			'pDayHostUsed' => $pDayHostUsed,
			'dayHostLeft' => $dayHostLeft,
			'expireDomain' => $expireDomain,
			'dayDomainUsed' => $dayDomainUsed,
			'pDayDomainUsed' => $pDayDomainUsed,
			'dayDomainLeft' => $dayDomainLeft,
			'samDomainHost' => HOST_DATE_REG == DOMAIN_DATE_REG && HOST_YEAR_REG == DOMAIN_YEAR_REG,
			'tables' => $this->Host->show(),
			'database_server' => array(),
			'web_server' => array(),
			'user_client' => array()
		);
		$data['database_server']['server_info'] = mysql_get_host_info();
		$data['database_server']['server_type'] = $this->db->dbdriver;
		$data['database_server']['server_version'] = mysql_get_server_info();
		$data['database_server']['proto_info'] = mysql_get_proto_info();
		$data['database_server']['server_user'] = $this->db->username.'@'.$this->db->hostname;
		$data['database_server']['server_charset'] = mysql_client_encoding();

		$data['web_server']['server_name'] = $this->input->server('SERVER_NAME');
		$data['web_server']['server_addr'] = $this->input->server('SERVER_ADDR');
		$data['web_server']['https'] = $this->input->server('HTTPS') ? 'On' : 'Off';
		$data['web_server']['server_software'] = $this->input->server('SERVER_SOFTWARE');
		$data['web_server']['server_protocol'] = $this->input->server('SERVER_PROTOCOL');
		$data['web_server']['document_root'] = $this->input->server('DOCUMENT_ROOT');

		$data['user_client']['user_agent'] = $this->input->server('HTTP_USER_AGENT');
		$data['user_client']['ip'] = $this->input->server('REMOTE_ADDR');
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Tình trạng hosting',
			'add_link' => '',
			'search' => false,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => false,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'], $data);
		$this->load->view('footer');
	}

	public function util_table()
	{
		/*
		* Kiem tra POST method
		*/
		if (!$_POST)
		{
			redirect();
		}
		/*
		* Kiem tra token va tham so yeu cau
		*/
		$table_name = $this->input->post('table_name', true);
		$op = $this->input->post('op', true);
		if (!$table_name || !$op || !$GLOBALS['per']['edit'])
		{
			echo 'Error';
			return false;
		}
		/*
		* Xu ly du lieu
		*/
		$data = $this->Dbs->util_table($op, $table_name);
		return 1;
	}
}

/* End of file hosting_status.php */
/* Location: ./application/controllers/hosting_status.php */